<?php

class marker extends MY_Controller{
	private $status = TRUE;
	private $token = '';
	
	function __construct(){
		parent::__construct();
		$this->load->database();
		Sys::loadLanguage($this->params['lang'],'panel');
	}

	public function _remap($method, $params){
		$res = $this->checkAccessToken();
		
		$this->load->model('user_model');
		$info = $this->user_model->getInfoUser($this->params);
		$this->params['usr_info'] = $info['user'];
		
		if($res && $this->params['usr_info']['status']==1){
			
			$this->load->model('user_model');
			$info = $this->user_model->getInfoUser($this->params);
			$this->params['usr_info'] = $info['user'];
			
			switch ($method) {
				case 'index':
						header('Location:'.Sys::$url_base.$this->params['lang_url'].'panel');
				break;
				case 'admin':
						$this->admin($method,$params);
				break;
				case 'del':
						$this->delete($method,$params);
				break;
				default:
					show_404();
				break;
			}
			
		}
		else if(isset($_COOKIE["p_goto"])){ //redireccionar a alguna url despues de logearse
			$url = $_COOKIE["p_goto"];
			setcookie('p_goto', '', time()-1200, '/');
			header("Location: ".$url);
		}else{
	    	header('Location:'.Sys::$url_base.$this->params['lang_url'].'');
		}
		
	}

	public function admin($method='', $params=''){
		$this->params['lang_txt_seccion'] = lang('txt_seccion_banner_admin');
		
		//CSS y JS Base
		$this->carabiner->css(array(
		    array('dev/style.css', 'screen', 'style.css'),
		    array('dev/panel/style.css', 'screen', 'panel-style.css'),
		    array('dev/panel/films/style_admin_films.css', 'screen', 'style_admin_films.css')
		));
		$this->carabiner->js(array(
			array('dev/libs/jquery.js'),
			array('dev/libs/jquery.tools.min.js'),
			array('dev/panel/general.js')
		));
		
		$this->load->model('marker_model');		
		$res_bann = $this->marker_model->get($this->params);
		
		if(is_array($res_bann['markers'])){
			$this->params['list'] = $res_bann;
		}

		// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
		$this->params['selected_menum2']		= 'mnu-selected';
		$this->params['selected_submenum21']	= 'submnu-selected';
		
		$this->params['seo'] = array(
			'title' 		=> ''.Sys::$title_web,
			'description' 	=> ''
		);
		
		$this->load->view('interface/header', $this->params);
		$this->load->view('interface/panel/content', $this->params);
		$this->load->view('interface/panel/markers/admin', $this->params);
		if($this->params['usr_info']['tipo']==='admin')
				$this->load->view('interface/panel/aside',$this->params);
		elseif($this->params['usr_info']['tipo']==='usuario')
			$this->load->view('interface/panel/user/aside',$this->params);
		$this->load->view('interface/footer', $this->params);
		
		
	}
	
	public function delete($method='', $params=''){
		$this->params['marker'] = $params[0];
		
		$response_errors = $this->validate('delete');
		if($this->status == TRUE){
			
			$this->load->model('marker_model');
			$film = $this->marker_model->delete($this->params);
			
			header('Location: '.Sys::$url_base.$this->params['lang_url'].'panel/marker/admin');
		}else{
			//CSS y JS Base
			$this->carabiner->css(array(
			    array('dev/style.css', 'screen', 'style.css'),
			    array('dev/panel/style.css', 'screen', 'panel-style.css')
			));
			$this->carabiner->js(array(
				array('dev/libs/jquery.js', 'screen', 'jquery.min.js'),
				array('dev/panel/general.js', 'screen', 'panel-general.js')
			));
			
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			if($this->params['usr_info']['tipo']==='admin')
				$this->load->view('interface/panel/aside',$this->params);
			elseif($this->params['usr_info']['tipo']==='usuario')
				$this->load->view('interface/panel/user/aside',$this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}



	
	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    protected function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	private function validate_delete(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate($this->params);
		
		$config	= array(
					array(
						'field' => 'marker',
						'label'	=> 'marker',
						'rules'	=> 'required|is_natural_no_zero'
					)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
}